package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PlayFrame extends JFrame {
    public PlayFrame(){
        this.setTitle("森林冰火人");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗体退出方式
        this.setSize(1030,900); //!!!

        Play firepeople = new Play();//火人
        firepeople.setX(550);
        firepeople.setY(700);//放到最底下
        Play icegem = new Play(); //冰钻石
        Play firegem = new Play(); //火钻石
        //firegem.setX(465);
        firegem.setY(800);//先把火钻石放到屏幕外面



        //将火人、火、冰钻石都添加到面板中
        PlayPanel panel = new PlayPanel(firepeople,firegem,icegem);


        Play play = new Play();

        this.add(panel);//把play添加到窗体

        this.setVisible(true); //把打开窗体显示移动到Play类实例化之后，防止窗体显示前，play对象没有被加载

        this.addKeyListener(new KeyListener() { //键盘监听
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                /*if(KeyEvent.VK_W ==e.getKeyCode()){ //上
                    play.setY(play.getY() - 10);
                }else if(KeyEvent.VK_S ==e.getKeyCode()){ //下
                    play.setY(play.getY() + 10);
                } */if(KeyEvent.VK_A ==e.getKeyCode()){//左
                    firepeople.setX(firepeople.getX() - 10);
                }else if(KeyEvent.VK_D ==e.getKeyCode()){//右
                    firepeople.setX(firepeople.getX() + 10);
                }else if(KeyEvent.VK_J ==e.getKeyCode()){ //发射冰钻石
                    //因为最开始冰钻石800的Y左边，玩家350的Y坐标
                    //按下J键，把当前玩家的左边赋值给冰钻石
                    //子弹的Y坐标此时小于800，开始移动
                    icegem.setY(firepeople.getY());
                    icegem.setX(firepeople.getX());

                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
         while(true){
            try {
                Thread.sleep(5);//画面每5ms秒刷新1次
            } catch (InterruptedException e) {
               // e.printStackTrace();
            }

            firegem.setX(firegem.getX()+1); //火钻石每次循环走1的距离

             if(firegem.getX()>800){
                 firegem.setX(0);//当超过屏幕的范围后，又从左边走
             }
             if(icegem.getY()<800){ //当冰钻石左边小于800，表示发射了冰钻石
                 icegem.setY(icegem.getY()-5);//冰钻石每次循环向上走5的距离

                 //火钻石矩形，通过当前钻石的坐标和大小来创建
                 Rectangle FgemRectangle =new Rectangle(firegem.getX(),
                         firegem.getY(),firegem.getWidth(),firegem.getHeight());
                 //冰钻石矩形，通过当前钻石的坐标和大小来创建
                 Rectangle IgemRectangle =new Rectangle(icegem.getX(),
                         icegem.getY(),icegem.getWidth(),icegem.getHeight());
                 //判断两个矩形是否相交，相交则跳出循环，让画面停住
                 if(FgemRectangle.intersects(IgemRectangle)){
                     break;
                 }
             }
            panel.repaint();//画面重新绘制
        }
    }

    public static void main(String[] args) {
        new PlayFrame(); //实例化窗体
    }
}
